package com.stevenmoon.csdnplus.net;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by WangJun on 2016/4/7.
 */
public class RequestParams {

    protected String url;
    protected final ConcurrentHashMap<String, String> urlParams = new ConcurrentHashMap<String, String>();

    public String getUrl() {
        return url;
    }

    public Map<String, String> getParamsMap() {
        return urlParams;
    }

    public RequestParams(String url) {
        this(url, null);
    }

    /**
     * Constructs a new empty {@code RequestParams} instance.
     */
    public RequestParams() {
        this(null, null);
    }

    /**
     * Constructs a new RequestParams instance containing the key/value string params from the
     * specified map.
     *
     * @param source the source key/value string map to add.
     * @param url    the url
     */
    public RequestParams(String url, Map<String, String> source) {
        this.url = url;
        if (source != null) {
            for (Map.Entry<String, String> entry : source.entrySet()) {
                put(entry.getKey(), entry.getValue());
            }
        }
    }


    public void put(Map<String, String> source) {
        if (source != null) {
            for (Map.Entry<String, String> entry : source.entrySet()) {
                put(entry.getKey(), entry.getValue());
            }
        }
    }

    /**
     * Adds a key/mutiple values string pair to the request.
     *
     * @param key   the key name for the new param.
     * @param values the mutiple values string list for the new param.
     */
    public void put(String key, List<String> values) {
        if (key != null && values != null) {
            for (String value : values){
                urlParams.put(key, value);
            }

        }
    }

    /**
     * Adds a key/value string pair to the request.
     *
     * @param key   the key name for the new param.
     * @param value the value string for the new param.
     */
    public void put(String key, String value) {
        if (key != null && value != null) {
            urlParams.put(key, value);
        }
    }

    /**
     * Adds a int value to the request.
     *
     * @param key   the key name for the new param.
     * @param value the value int for the new param.
     */
    public void put(String key, int value) {
        if (key != null) {
            urlParams.put(key, String.valueOf(value));
        }
    }

    /**
     * Adds a long value to the request.
     *
     * @param key   the key name for the new param.
     * @param value the value long for the new param.
     */
    public void put(String key, long value) {
        if (key != null) {
            urlParams.put(key, String.valueOf(value));
        }
    }


}
